package niuke.week01;

import java.util.*;

/**
 用x,y表示一个整数范围区间，现在输入一组这样的范围区间(用空格隔开)，请输出这些区间的合并。
 输入
 1,3 2,5 8,10 11,15
 输出
 1,5 8,10 11,15

 */
public class CombinQUJian {

    public static  class Pair{
        int i;
        int j;

        public Pair(int i, int j) {
            this.i = i;
            this.j = j;
        }
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();

        String[] quJian = str.split(" ");

        PriorityQueue<Pair> pairs = new PriorityQueue<>(quJian.length, new Comparator<Pair>() {
            @Override
            public int compare(Pair o1, Pair o2) {
                return o1.i - o2.i;
            }
        });

        for (String s : quJian){
            String[] split = s.split(",");
            String i = String.valueOf(split[0]);
            String j = String.valueOf(split[1]);
            pairs.add(new Pair(Integer.valueOf(i),Integer.valueOf(j)));
        }

        Set<Pair> set = new HashSet<>();

        Pair prePair = pairs.poll();
        while (!pairs.isEmpty()){
            Pair pair = pairs.poll();
            System.out.println(pair.i+":"+pair.j);
            assert prePair != null;
            if(prePair.j>=pair.j){//[3,5]   [3,2]  -->   [3,5]  舍弃
                continue;
            }else if(prePair.j<pair.i){//[3,5]   [7,8]   不变
                set.add(prePair);
                prePair=pair;
            }else{//[3,5]   [4,8]  --》 [3,8]  合并
                prePair=new Pair(prePair.i, pair.j);;
            }
        }
        set.add(prePair);

        PriorityQueue<Pair> resQue = new PriorityQueue<>(quJian.length, new Comparator<Pair>() {
            @Override
            public int compare(Pair o1, Pair o2) {
                return o1.i - o2.i;
            }
        });
        resQue.addAll(set);

        while (!resQue.isEmpty()){
            Pair poll = resQue.poll();
            System.out.print(poll.i+","+poll.j+" ");
        }

    }
}